#!coding:utf-8
'''
Created on Jan 21, 2013
易宝支付测试集合
@author: join
'''
from pyqt.pay.yeepay import YeepayClient
from pyqt.pay import ISO8583
import ConfigParser
import os

server_ip = '119.161.147.124'
server_port = 28000
server_port2 = 29000

class YeepayTestSuite(object):
    '''
    易宝支付测试集合
    '''

    def __init__(self):
        pass
        
    def _get_main_key(self):
        try:
            file_handler = open('mainkey')
            lines = file_handler.readlines()
            mainkey = ''
            for line in lines:
                if line.startswith('终端主密钥密文'):
#                    print 'line is:',line
                    mainkey = line.split('=')[1]
#                    print 'mainkey is:',mainkey
            return mainkey
        except Exception as e:
            print 'exception is:',e
        finally:
            file_handler.close()
    
    def test_inject_mainkey(self):
        YeepayClient._SERVER_IP = server_ip
        YeepayClient._SERVER_PORT2 = server_port2
        client = YeepayClient()
        mainkey = client.pos_request()
        mainkey_chinese = mainkey.decode('hex').decode('gb2312')
        try:
            with file('mainkey2','w') as f:
                f.write(mainkey_chinese)
        except Exception as e:
            print 'exception is:',e
        finally:
            f.close()
            
    def test_sign_in(self):
        YeepayClient._SERVER_IP = server_ip
        YeepayClient._SERVER_PORT2 = server_port
        YeepayClient._YEEPAY_TERMINAL_NUMBER='60101008'
        YeepayClient._YEEPAY_CUSTOMER_NUMBER='874110145112480'
        client = YeepayClient()
        
        client.signin()
        
    def test_consume(self):
        YeepayClient._SERVER_IP = server_ip
        YeepayClient._SERVER_PORT2 = server_port
        YeepayClient._YEEPAY_TERMINAL_NUMBER='60101008'
        YeepayClient._YEEPAY_CUSTOMER_NUMBER='874110145112480'
        client = YeepayClient()
        
        client.consume(2, '', '') #TODO:

    def test_balance(self):
        YeepayClient._SERVER_IP = server_ip
        YeepayClient._SERVER_PORT2 = server_port
        YeepayClient._YEEPAY_TERMINAL_NUMBER='60101008'
        YeepayClient._YEEPAY_CUSTOMER_NUMBER='874110145112480'
        client = YeepayClient()
        
        client.balance(100,1)
        
    def test_rollback(self):
        YeepayClient._SERVER_IP = server_ip
        YeepayClient._SERVER_PORT2 = server_port
        YeepayClient._YEEPAY_TERMINAL_NUMBER='60101008'
        YeepayClient._YEEPAY_CUSTOMER_NUMBER='874110145112480'
        client = YeepayClient()
        
        client.rollback_consume()
        
if __name__ == '__main__':
    testsuite = YeepayTestSuite()
    testsuite.test_inject_mainkey()
#    testsuite.test_rollback()
#    testsuite._get_main_key()
#    testsuite.test_balance()

    
    
    
